package com.yl.zqy.service.impl.sys;

import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yl.zqy.common.Result;
import com.yl.zqy.constant.ZQYConstant;
import com.yl.zqy.dto.SysDeptDTO;
import com.yl.zqy.dto.SysMenuDTO;
import com.yl.zqy.entity.sys.SysDept;
import com.yl.zqy.mapper.sys.SysDeptMapper;
import com.yl.zqy.service.sys.SysDeptService;
import com.yl.zqy.utils.ConvertUtils;
import com.yl.zqy.utils.TreeUtils;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 部门管理 服务实现类
 * </p>
 *
 * @author liang.yu
 * @since 2024-03-24
 */
@Service
@RequiredArgsConstructor
public class SysDeptServiceImpl extends ServiceImpl<SysDeptMapper, SysDept> implements SysDeptService {
    @Override
    public Result<Boolean> insertDept(SysDept dept) {
        dept.setPids(getPidList(dept.getPid()));
        int res = baseMapper.insert(dept);
        if (res > 0) {
            return Result.ok(true);
        }
        return Result.error(false, "新增失败！");
    }

    @Override
    public Result<Boolean> updateDpet(SysDept dept) {
        dept.setPids(getPidList(dept.getPid()));
        int res = baseMapper.updateById(dept);
        if (res > 0) {
            return Result.ok(true);
        }
        return Result.error(false, "修改失败！");
    }

    @Override
    public Result<Boolean> deleteById(String id) {
        int res = baseMapper.deleteById(id);
        if (res > 0) {
            return Result.ok(true);
        }
        return Result.error(false, "操作失败！");
    }

    @Override
    public List<String> getSubDeptIdList(String id) {
        List<String> deptIdList = baseMapper.getSubDeptIdList(id);
        deptIdList.add(id);
        return deptIdList;
    }

    @Override
    public List<SysDeptDTO> deptList(String name) {
        //查询部门列表
        List<SysDept> entityList = baseMapper.deptList(null, name);
        List<SysDeptDTO> dtoList = ConvertUtils.sourceToTarget(entityList, SysDeptDTO.class);
        return TreeUtils.build(dtoList);
    }

    @Override
    public List<SysDeptDTO> deptSelectTree() {
        //查询部门列表
        List<SysDept> entityList = baseMapper.deptList(null,null);
        List<SysDeptDTO> dtoList = ConvertUtils.sourceToTarget(entityList, SysDeptDTO.class);
        SysDeptDTO sysDeptDTO = new SysDeptDTO();
        sysDeptDTO.setPid("top");
        sysDeptDTO.setId("0");
        sysDeptDTO.setName("根目录");
        dtoList.add(0, sysDeptDTO);
        return TreeUtils.build(dtoList);
    }

    /**
     * 获取所有上级部门ID
     *
     * @param pid 上级ID
     */
    private String getPidList(String pid) {
        //顶级部门，无上级部门
        if (ZQYConstant.DEPT_ROOT.equals(pid)) {
            return ZQYConstant.DEPT_ROOT + "";
        }
        //所有部门的id、pid列表
        List<SysDept> deptList = baseMapper.getIdAndPidList();
        //list转map
        Map<String, SysDept> map = new HashMap<>(deptList.size());
        for (SysDept entity : deptList) {
            map.put(entity.getId(), entity);
        }
        //递归查询所有上级部门ID列表
        List<String> pidList = new ArrayList<>();
        getPidTree(pid, map, pidList);

        return StringUtils.join(pidList, ",");
    }

    private void getPidTree(String pid, Map<String, SysDept> map, List<String> pidList) {
        //顶级部门，无上级部门
        if (ZQYConstant.DEPT_ROOT.equals(pid)) {
            return;
        }
        //上级部门存在
        SysDept parent = map.get(pid);
        if (parent != null) {
            getPidTree(parent.getPid(), map, pidList);
        }
        pidList.add(pid);
    }
}
